/**
 * @author Shen Yi - bm2736892@gmail.com
 */

var TextureManager = {
	
	_textures : [],
	_samplers : [],
	_renderSurfaceSets : [],
	
	_pack : null,
	_loader : null,
	_o3d : null,
	
	/**
	 * 
	 * @param {Object} pack
	 * @param {Object} o3d
	 */
	init : function(pack, o3d){
		this._pack = pack;
		this._o3d = o3d;
	},
	/**
	 * 
	 * @param {String} name
	 * @param {Object} sampler
	 */
	addSampler : function(name, sampler){
		this._samplers[name] = sampler;
		if(isset(sampler.texture)){
			this._textures[name] = sampler.texture;
		}
	},
	/**
	 * 
	 * @param {String} name
	 */
	getTexture : function(name){
		if (isset(name)) {
			return this._textures[name];
		}
		return this._textures;	
	},
	
	/**
	 * 
	 * @param {String} name
	 */
	getSampler : function(name){
		if(isset(name)){
			return this._samplers[name];
		}
		return this._samplers;
	},
	
	/**
	 * 
	 * @param {String} name
	 */
	getRenderSurfaceSet : function(name){
		return this._renderSurfaceSets[name];
	},
	
	/**
	 * 
	 * @param {Function} callback
	 */
	createLoader : function(callback){
		this._loader = o3djs.loader.createLoader(callback);
		
		return this._loader;
	},
	
	/**
	 * 
	 * @param {String} name
	 * @param {Object} options
	 * @return {Sampler}
	 */
	createSampler : function(name, options, callback){
		if(isset(this._samplers[name])){
			return this._samplers[name];
		}
		var sampler = this._pack.createObject("Sampler");
		
		sampler.name = name;
		
		if(isset(options.minFilter) && options.minFilter){
			eval("sampler.minFilter = this._o3d.Sampler." + options.minFilter);
		}
		if(isset(options.magFilter) && options.magFilter){
			eval("sampler.magFilter = this._o3d.Sampler." + options.magFilter);
		}
		if(isset(options.mipFilter) && options.mipFilter){
			eval("sampler.mipFilter = this._o3d.Sampler." + options.mipFilter);
		}
		if(isset(options.addressModeU) && options.addressModeU){
			eval("sampler.addressModeU = this._o3d.Sampler." + options.addressModeU);
		}
		if(isset(options.addressModeV) && options.addressModeV){
			eval("sampler.addressModeV = this._o3d.Sampler." + options.addressModeV);
		}
		
		this._samplers[name] = sampler;
		
		//load the texture from file
		//if the texture is a render target, it is not needed
		if(isset(options.src) && options.src){
			this._loader.loadTexture(this._pack, options.src, function(texture, exception){
				if(exception){
					alert(exception);
				}else{
					TextureManager._textures[name] = texture;
					sampler.texture = texture;
				}
				if(isset(callback)){
					callback(sampler);
				}
			})
		}
		return sampler;
	},
	
	/**
	 * 
	 * @param {String} name
	 * @param {Integer} width
	 * @param {Integer} height
	 * @return {Texture2D} 
	 */
	createRenderTarget2D : function(name, width, height, format){
		eval("var format = this._o3d.Texture."+format)
		var texture2d = this._pack.createTexture2D(width, height, format, 1, true);
		texture2d.name = name;
		
		var renderSurface = texture2d.getRenderSurface(0, this._pack);
		
		var depthSurface = this._pack.createDepthStencilSurface(width, height);
		
		var renderSurfaceSet = this._pack.createObject("RenderSurfaceSet");
		renderSurfaceSet.renderSurface = renderSurface;
		renderSurfaceSet.renderDepthStencilSurface = depthSurface;
		
		this._textures[name] = texture2d;
		this._renderSurfaceSets[name] = renderSurfaceSet; 
		return texture2d;
	},
	
	reset : function(){
		this._textures = [],
		this._renderSurfaceSets = [],
		this._samplers = []
	}
}